package com.dld.coupon.db;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.dld.android.util.LogUtils;
import com.dld.coupon.activity.R;
import com.dld.coupon.util.MapUtil;

public class Address {
	private static List<String> list;

	public static List<String> getAddresses(SQLiteDatabase paramSQLiteDatabase,
			String paramString1, String paramString2) {
		ArrayList<String> localArrayList = new ArrayList();
		localArrayList.add("全" + paramString2);
		Object[] localObject1 = new String[1];
		localObject1[0] = "name";
		Object[] localObject2 = new String[2];
		localObject2[0] = paramString1;
		localObject2[1] = paramString2;
		Cursor cursor = paramSQLiteDatabase.query("address",
				(String[]) localObject1, "city = ? AND district = ?",
				(String[]) localObject2, null, null, null);

		while (cursor.moveToNext()) {
			String name = cursor.getString(0);
			localArrayList.add(name);
		}
		cursor.close();
		return localArrayList;

	}

	public static String getDistrict(SQLiteDatabase paramSQLiteDatabase,
			String paramString1, String paramString2) {
		String str = null;
		Cursor localCursor = paramSQLiteDatabase.rawQuery(
				"SELECT district FROM address WHERE city = '" + paramString1
						+ "' AND name = '" + paramString2 + "'", null);
		LogUtils.log("test", "SELECT district FROM address WHERE city = '"
				+ paramString1 + "' AND name = '" + paramString2 + "'");
		if (localCursor.moveToFirst())
			str = localCursor.getString(0);
		localCursor.close();
		return str;
	}

	/**
	 * obtain the city id according to the cityname
	 * 
	 * @param paramSQLiteDatabase
	 * @param paramString
	 * @return
	 */
	public static String getCityIdByName(SQLiteDatabase paramSQLiteDatabase,
			String paramString) {
		Cursor cursor = null;
		try {
			cursor = paramSQLiteDatabase.query("citylist", null,
					"cityname = ?", new String[] { paramString }, null, null,
					null);
			while (cursor.moveToNext()) {
				return cursor.getString(cursor.getColumnIndex("cityid"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cursor.close();
		}
		return null;
	}

	public static String queryCityIdByTopIdAndName(SQLiteDatabase sdb,
			String topid, String name) {
		Cursor cursor = null;

		try {
			cursor = sdb.query("citylist", null, "topid = ? and cityname= ?",
					new String[] { topid, name }, null, null, null);
			while (cursor.moveToNext()) {
				Log.v("", "queryCityIdByTopIdAndName **********   "
						+ topid
						+ " :"
						+ cursor.getString(cursor
								.getColumnIndexOrThrow("cityid")));
				return cursor.getString(cursor.getColumnIndexOrThrow("cityid"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cursor.close();
		}
		return null;
	}

	public static List<String> getDistricts(SQLiteDatabase paramSQLiteDatabase,
			String paramString) {

		list = new ArrayList();
		String id = getCityIdByName(paramSQLiteDatabase, paramString);

		Cursor cursor = null;
		Log.v("", "**************  city name hot district  " + paramString
				+ " , id  " + id);

		try {
			cursor = paramSQLiteDatabase.query("citylist", null, "topid = ?",
					new String[] { id }, null, null, null);
			Log.v("", "add district cityid   *****  " + id);
			while (cursor.moveToNext()) {
				list.add(cursor.getString(cursor
						.getColumnIndexOrThrow("cityname")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cursor.close();
		}
		return list;
	}

	public static List<String> getHotAddresses(
			SQLiteDatabase paramSQLiteDatabase, String paramString) {
		ArrayList<String> localArrayList;
		if (paramString.equals("北京")) {
			localArrayList = new ArrayList<String>();
			localArrayList.add("全北京 ");
			localArrayList.add("和平里");
			localArrayList.add("王府井");
			localArrayList.add("建国门");
			localArrayList.add("东单");
			localArrayList.add("西单");
			localArrayList.add("朝阳门");
			localArrayList.add("菜市口");
			localArrayList.add("崇文门 ");
			localArrayList.add("三里屯");
			localArrayList.add("国贸 ");
			localArrayList.add("双井 ");
			localArrayList.add("工体北门 ");
		} else {
			// return localArrayList;
			localArrayList = new ArrayList();
			localArrayList.add("全" + paramString);
			String[] arrayOfString = new String[1];
			arrayOfString[0] = paramString;
			Cursor cursor = paramSQLiteDatabase.query("address",
					new String[] { "name" }, "city = ?", arrayOfString, null,
					null, null, "9");
			try {
				while (cursor.moveToNext()) {
					localArrayList.add(cursor.getString(0));
				}
				cursor.close();
			} catch (Exception localException) {
				LogUtils.e("test", "", localException);
			}
		}
		return localArrayList;
	}

	public static void init(Context paramContext,
			SQLiteDatabase paramSQLiteDatabase) {

		Log.v("", "init **************            ");
		try {
			BufferedReader localBufferedReader = new BufferedReader(
					new InputStreamReader(paramContext.getResources()
							.openRawResource(R.raw.address), "utf-8"), 1024);
			while (true) {
				String str = localBufferedReader.readLine();
				if (str == null)
					break;
				paramSQLiteDatabase.execSQL(str);
			}
		} catch (Exception localException) {
			LogUtils.e("test", "", localException);
		}
	}
}
